<?php
namespace app\admin\controller\house;

use app\admin\controller\Common;
use app\admin\library\Property;
use app\admin\model\HouseBilling as BillingModel;
use app\admin\model\HouseNumber as NumberModel;
use app\admin\model\HouseProperty as PropertyModel;

class Collected extends Common
{
    public function index()
    {
        $loginUser = $this->auth->getLoginUser();
        $house_property_id = $this->request->param('house_property_id/d', Property::getProperty($loginUser['id']));
        $properties = PropertyModel::where('admin_user_id', $loginUser['id'])->all();
        $property = [];
        foreach ($properties as $v) {
            if ($v['id'] === $house_property_id) {
                $property = $v;
                break;
            }
        }
        $billing = BillingModel::where('a.house_property_id', $house_property_id)
        ->alias('a')
        ->join('HouseNumber b', 'a.house_property_id = b.house_property_id and a.house_number_id = b.id')
        ->field('a.*,b.name as number_name')
        ->where('accounting_date', 'not null')
        ->order(['accounting_date' => 'desc','number_name'])
        ->paginate(10);
        $sum = BillingModel::where('house_property_id', $house_property_id)
        ->whereTime('accounting_date', 'today')
        ->sum('total_money');
        $this->assign('property', $property);
        $this->assign('sum', $sum);
        $this->assign('properties', $properties);
        $this->assign('house_property_id', $house_property_id);
        $this->assign('billing', $billing);
        return $this->fetch();
    }
}
